package com.jjw.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jjw.route.dao.PtypeDao;
import com.jjw.route.entity.PtypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.Query;

import com.jjw.route.dao.PoiDao;
import com.jjw.route.entity.PoiEntity;
import com.jjw.route.service.PoiService;


@Service("poiService")
public class PoiServiceImpl extends ServiceImpl<PoiDao, PoiEntity> implements PoiService {

    @Autowired
    PoiDao poiDao;
    @Autowired
    PtypeDao ptypeDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PoiEntity> page = this.page(
                new Query<PoiEntity>().getPage(params),
                new QueryWrapper<PoiEntity>()
        );

        return new PageUtils(page);
    }

    // 发布文章 更新 poi 数据
    @Override
    public PoiEntity poiArticalRealseUpdate(String name, int time, int imgNumber) {
        PoiEntity poiEntity = poiDao.selectOne(new QueryWrapper<PoiEntity>().eq("name", name));
        //更新所有同属类型的 总照片数目
        PtypeEntity ptypeEntity = ptypeDao.selectById(poiEntity.getTypeid());
        ptypeEntity.setPnumber(ptypeEntity.getPnumber() + imgNumber);
        ptypeDao.updateById(ptypeEntity);
        //流行度、平均时间更新
        poiEntity.setPnumber(poiEntity.getPnumber() + imgNumber);
        poiEntity.setTime(poiEntity.getTime() + time);
        poiEntity.setFrequency(poiEntity.getFrequency() + 1);
        //流行度
        poiEntity.setPrevalence((float) ((poiEntity.getPnumber() / ptypeEntity.getPnumber()) * 10000));
        //平均时间
        poiEntity.setVtime(poiEntity.getTime() / poiEntity.getFrequency());
        poiDao.updateById(poiEntity);
        return poiEntity;
    }

    @Override
    public List<PoiEntity> getPrevalenceRank() {
        List<PoiEntity> poiEntities = poiDao.selectList(new QueryWrapper<>());
        Comparator<PoiEntity> poiEntityComparator = new Comparator<PoiEntity>(){
            @Override
            public int compare(PoiEntity o1, PoiEntity o2) {
                return (int) (o1.getPrevalence() - o2.getPrevalence());
            }
        };
        List<PoiEntity> pois = new ArrayList<>();
        for (int i = 0; i < 10; i++) pois.add(poiEntities.get(i));
        return pois;
    }

    @Override
    public List<PoiEntity> getAllPoi() {
        return poiDao.selectList(new QueryWrapper<>());
    }

    @Override
    public List<PoiEntity> getTopPoi() {
        List<PoiEntity> poiEntities = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            poiEntities.add(poiDao.selectById(random.nextInt(77) + 1));;
        }
        return poiEntities;
    }

    @Override
    public List<PoiEntity> selectByNameList(ArrayList<String> spots_name) {
        return poiDao.selectList(new QueryWrapper<PoiEntity>().in("name",spots_name));
    }

    @Override
    public PoiEntity selectByName(String spot_name) {
        return poiDao.selectOne(new QueryWrapper<PoiEntity>().eq("name",spot_name));
    }

    @Override
    public List<PoiEntity> selectList(LambdaQueryWrapper<PoiEntity> wrapper) {
        return poiDao.selectList(wrapper);
    }

}